<?php
class PanelSelectIDFromTable extends PageElement
{
	protected $Database;
	protected $Table;
	protected $RecordID;
	protected $ParentTable;
	protected $parentParentTable;
	protected $ReadOnly=false;
	protected $DateDisabler=false;
	
	public function setReadOnly($readOnly)
	{
		$this->ReadOnly = $readOnly;
	}
	
	public function setDateDisabler($dateDisabler)
	{
		$this->DateDisabler = $dateDisabler;
	}
	
	public function __Construct($Database,$Table,$RecordID,$parentTable,$parentParentTable)
	{
//		echo "<br>New PanelSelectIDFromTable Table=".$Table."    RecordID = ".$RecordID."<br>";
		$this->Database = $Database;
		$this->Table = $Table;
		$this->RecordID = $RecordID;
		$this->ParentTable = $parentTable;
		$this->parentParentTable = $parentParentTable;
		//		echo "RecordID = ".$RecordID;
	}

	public function getHTML()
	{
		$dbTable = new DBTable($this->Database,$this->Table);
		$tableStructure = $dbTable->getTableStructure();
		$keys = array_keys($tableStructure);
		$primaryKey = $dbTable->getPrimaryKey();
		
		if($this->ReadOnly)
		{	
//			echo "IDfromTable ID = ".$this->RecordID."<br>";
			$sql = "SELECT * FROM ".$this->Table." WHERE ".substr($this->Table,0,-1)."ID = ".$this->RecordID;
			$this->Database->ExecuteSQL($sql);
//			echo $sql."<br>";
			$row = $this->Database->getRow();
//			print_r($row);
//			echo "<br>";
			
			$html = "<table>\n";
			$html .= "<tr><td colspan=\"2\"><b>".substr($this->Table,0,-1)."</b></td></tr>";
			foreach($keys as $key)
			{
				if($key == $primaryKey)continue;
				if(substr($key,-3)=="IDP")continue;
				$html .= "<tr><td><b>".$key."</b></td><td>".$row[$key]."</td></tr>\n";
			}
		
			$html .= "</table>\n";
		}
		else
		{
			$string = substr($this->parentParentTable,0,-1);
			$sql  = "SELECT * FROM ".$this->Table." AS t ";
			$sql .= "INNER JOIN ".$this->parentParentTable." AS ppt ";
			$sql .= "ON t.".$string."IDP = ppt.".$string."ID ";
			$sql .= "WHERE ppt.DeletedBy = '0'";
//			echo $sql."<br>\n";
			$this->Database->ExecuteSQL($sql);
			$html = "<script language=\"javascript\">\nvar dataArray = new Array();\n";
			$html .= "var dataKeys = new Array();\n";
			$html .= "var dataIndices = new Array();\n";
			$i = 0;
			foreach($keys as $key)
				{
					if($key == $primaryKey)continue;
					if(substr($key,-3)=="IDP")continue;
					$html .= "dataKeys[$i] = '$key';\n";
					$i++;
				}
			
			$totalrecords=0;
			while($row = $this->Database->getRow())
			{
				$html .= "dataIndices[$totalrecords] = ".$row[$primaryKey].";\n";
				foreach($keys as $key)
				{
//					echo $key."<br>\n";
					if($key == $primaryKey)continue;
					if(substr($key,-3)=="IDP")continue;
					// strip newlines from value
					$text = $row[$key];
					$text = str_replace("\n", "", $text);
					$text = str_replace("\r", "", $text);
					$text = str_replace("0001-01-01", "", $text);
					
					$html .= "dataArray['".$row[$primaryKey].$key."'] = '".addslashes($text)."';\n";
				}
				$totalrecords++;
			}
			
			$html .= "function displayRecord(recordID)\n";
			$html .= "{\n";
			$html .= "//alert(recordID);\n";
			$html .= "for(key in dataKeys)\n";
			$html .= "{\n";
			$html .= "//alert(dataKeys[key]+'Value');\n";
			$html .= "//alert(dataIndices[recordID]);\n";
			$html .= "//alert(dataIndices[recordID]+dataKeys[key]);\n";
			$html .= "//alert(dataArray[dataIndices[recordID]+dataKeys[key]]);\n";
			$html .= "document.getElementById(dataKeys[key]+'Value').innerHTML=dataArray[dataIndices[recordID]+dataKeys[key]];\n";
			$html .= "document.getElementById('InsertRecord_".$this->ParentTable."_".$primaryKey."T').value=dataIndices[recordID];\n";
			$html .= "}\n";
			$html .= "\n";
			$html .= "\n";
			$html .= "\n";
			$html .= "\n";
			$html .= "}\n";
			
			$html .= "function previousRecord(jump)\n";
			$html .= "{\n";
			//$html .= "alert(jump)\n";
			$html .= "if(document.getElementById('SelectedRecord').value=='-1')displayRecord(0);\n";
			$html .= "selectedRecord = parseInt(document.getElementById('SelectedRecord').value);\n";
			$html .= "if(isNaN(selectedRecord))selectedRecord=0;\n";
			$html .= "totalRecords = document.getElementById('TotalRecords').value;\n";
			$html .= "if(selectedRecord-jump>=0)\n";
			$html .= "{\n";
			$html .= "selectedRecord=selectedRecord-jump;\n";
			$html .= "document.getElementById('SelectedRecord').value = selectedRecord;\n";
			$html .= "displayRecord(selectedRecord);\n";
			$html .= "}\n";
			$html .= "else\n";
			$html .= "{\n";
			$html .= "selectedRecord=0;\n";
			$html .= "document.getElementById('SelectedRecord').value = selectedRecord;\n";
			$html .= "displayRecord(selectedRecord);\n";
			$html .= "}\n";
//			$html .= "alert(selectedRecord)\n";
			$html .= "\n";
			$html .= "\n";
			$html .= "}\n";
			
			$html .= "function nextRecord(jump)\n";
			$html .= "{\n";
			//$html .= "alert(jump)\n";
			$html .= "if(document.getElementById('SelectedRecord').value=='-1')displayRecord(0);\n";
			$html .= "selectedRecord = parseInt(document.getElementById('SelectedRecord').value);\n";
//			$html .= "alert(selectedRecord)\n";
			$html .= "if(isNaN(selectedRecord))selectedRecord=0;\n";
			$html .= "totalRecords = document.getElementById('TotalRecords').value;\n";
			$html .= "if(selectedRecord+jump<=totalRecords-1)\n";
			$html .= "{\n";
			$html .= "selectedRecord=selectedRecord+jump;\n;";
			$html .= "document.getElementById('SelectedRecord').value = selectedRecord\n";
			$html .= "displayRecord(selectedRecord);\n";
			$html .= "}\n";
			$html .= "else\n";
			$html .= "{\n";
			$html .= "selectedRecord=totalRecords-1;\n;";
			$html .= "document.getElementById('SelectedRecord').value = selectedRecord\n";
			$html .= "displayRecord(selectedRecord);\n";
			$html .= "}\n";
//			$html .= "alert(selectedRecord)\n";
			$html .= "\n";
			$html .= "}\n";
			
			$html .= "</script>\n";
			
			$previous10Button = new Button();
			$previous10Button->setOnclick("previousRecord(10);");
			$previous10Button->setName("Previous10");
			$previous10Button->setValue("<10");
			$html .= $previous10Button->getHTML();
			
			$previous1Button = new Button();
			$previous1Button->setOnclick("previousRecord(1);");
			$previous1Button->setName("Previous1");
			$previous1Button->setValue("<1");
			$html .= $previous1Button->getHTML();
			
			$next1Button = new Button();
			$next1Button->setOnclick("nextRecord(1);");
			$next1Button->setName("Next1");
			$next1Button->setValue(">1");
			$html .= $next1Button->getHTML();
			
			$next10Button = new Button();
			$next10Button->setOnclick("nextRecord(10);");
			$next10Button->setName("Next10");
			$next10Button->setValue(">10");
			$html .= $next10Button->getHTML();
			
			$hidden = new Hidden();
			$hidden->setName('SelectedRecord');
//			echo "recordID = ".$this->RecordID."<br>\n";
			$string = $this->RecordID-1;
//			echo "recordID* = ".$string."<br>\n";
			
			if($this->RecordID==-1)
				$hidden->setValue(-1);
			else
				$hidden->setValue($string);
			$html .= $hidden->getHTML();
			
			$hidden = new Hidden();
			$hidden->setName('TotalRecords');
			$hidden->setValue($totalrecords);
			$html .= $hidden->getHTML();
			
			$hidden = new Hidden();
			$hidden->setName('PrimaryKey');
			$hidden->setValue($primaryKey);
			$html .= $hidden->getHTML();
			
			$hidden = new Hidden();
			$hidden->setName('InsertRecord_'.$this->ParentTable.'_'.$primaryKey."T");
			$hidden->setValue($this->RecordID);
			$html .= $hidden->getHTML();
				
			$html .= "<table>\n";
			$html .= "<tr><td colspan=\"2\"><b>".substr($this->Table,0,-1)."</b></td></tr>";

//			If RecordID is set, get the values

			$sql = "SELECT * FROM ".$this->Table." WHERE ".substr($this->Table,0,-1)."ID = ".$this->RecordID;
			$this->Database->ExecuteSQL($sql);
//			echo $sql."<br>";
			$row = $this->Database->getRow();
//			print_r($row);
//			echo "<br>";
			
			foreach($keys as $key)
			{
				if($key == $primaryKey)continue;
				if(substr($key,-3)=="IDP")continue;
				$fieldname=new Span();
				$fieldname->setName($key);
				$fieldname->setText($key);
				$value=new Span();
				$value->setName($key.'Value');
				$value->setText($row[$key]);
				$html .= "<tr><td><b>".$fieldname->getHTML()."</b></td><td>".$value->getHTML()."</td></tr>\n";
			}
		
			$html .= "</table>\n";
		}
			
		return $html;
	}
	
}

?>